'use strict';

const Service = require('egg').Service;
const ulid = require('../public/ulid');
const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require('../oltmodules/HuaWeiOLT');
const XDOLT = require('../oltmodules/XinDeOLT');
const XGLOLT = require('../oltmodules/XinGeLinOLT');
class OnuProfileService extends Service {
  async list(params) {
    let result = {
      count:"",
      rows:""
    }
    const where = {};
    where.oltId = params.OltId
    if(params.search){
      where.Pname = new RegExp(params.search)
    }
    result.count = await this.ctx.model.OnuProfile.find(where).count('_id')
    result.rows = await this.ctx.model.OnuProfile.find(where).collation({"locale": "zh", numericOrdering:true}).sort({Pname:1}).skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    return result
  }
  async allList(params) {
    let result = await this.ctx.model.OnuProfile.find({oltId:params.OltId}).collation({"locale": "zh", numericOrdering:true}).sort({Pname:1})
    return result
  }
  async create(params) {
    let result = {
      msg:"",
      flag:false
    }
    try {
      let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
      await onuProfileCreate(oltResult,params,this)
      let onuProfileResult = await this.ctx.model.OnuProfile.findOne({Pname:params.name,oltId:params.oltId})
      if(onuProfileResult){
        await this.ctx.model.OnuProfile.updateOne({_id:onuProfileResult._id},{$set:{Pname:params.name,description:params.description}})
      }else{
        await this.ctx.model.OnuProfile.create({_id:ulid.ulid(),Pname:params.name,description:params.description,oltId:params.oltId})
      }
      result.msg = "create onuprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "create onuprofile fail,"+error
      result.flag = false
      return result
    }
  }
  async edit(params) {
    let result = {
      msg:"",
      flag:false
    }
    try {
      let onuProfileResult = await this.ctx.model.OnuProfile.findOne({_id:params.id})
      let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
      await onuProfileEdit(onuProfileResult,params,oltResult,this)
      await this.ctx.model.OnuProfile.updateOne({_id:params.id},{$set:{description:params.description}})
      result.msg="edit onuprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg="edit onuprofile fail,"+error
      result.flag = false
      return result
    }
  }
  async delete(params) {
    let result = {
      msg:"",
      flag:false
    }
    try {
      let onuProfileResult = await this.ctx.model.OnuProfile.findOne({_id:params.id})
      let oltResult = await this.ctx.model.Olt.findOne({_id:onuProfileResult.oltId})
      await onuProfileDelete(onuProfileResult,oltResult,this)
      await this.ctx.model.OnuProfile.deleteOne({_id:params.id})
      result.msg="delete onuprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg="delete onuprofile fail,"+error
      result.flag = false
      return result
    }
  }
  async getOnuProfileMsg(params) {
    let result = {
      msg:"",
      data:"",
      flag:false
    }
    let oltTypeResult = await this.ctx.model.OnuProfile.findOne({_id:params.id})
    let oltResult = await this.ctx.model.Olt.findOne({_id:oltTypeResult.oltId})
    try {
      let info = await getOnuProfileMsg(oltTypeResult,oltResult,this)
      result.msg = "getOltType success",
      result.data = info
      result.flag = true
      return result
    } catch (error) {
      result.msg = "getOltType fail,"+error
      result.flag = false
      result.data = ""
      return result
    }
  }
}
async function onuProfileCreate(oltInformation,params,me) {
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.onuProfileCreate(params.eth,params.pots,params.rf,params.name,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.onuProfileCreate(params.eth,params.pots,params.rf,params.name,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function onuProfileDelete(onuProfileResult,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.onuProfileDelete(onuProfileResult,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.onuProfileDelete(onuProfileResult,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function getOnuProfileMsg(onuProfileResult,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.getOnuProfileMsg(onuProfileResult,oltInformation.oltHardVersion,me)
        break;
        case "HUAWEI":
        const hw = new HWOLT(options);
        result = await hw.getOnuProfileMsg(onuProfileResult,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function onuProfileEdit(onuProfileResult,params,oltInformation,me){
  const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
  let result
  try {
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.onuProfileEdit(onuProfileResult,params.eth,params.pots,params.rf,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.onuProfileEdit(onuProfileResult,params.eth,params.pots,params.rf,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}

module.exports = OnuProfileService;
